package cn.tsoft.taobao.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import cn.tsoft.taobao.adapter.ProductItemAdapter;
import cn.tsoft.taobao.logic.ITaoBaoActivity;
import cn.tsoft.taobao.logic.Task;
import cn.tsoft.taobao.logic.TaskListActivity;

public class ProductItem extends  TaskListActivity  implements ITaoBaoActivity{
	   public static final int  REFRESH_PRODUCT=1;
	    public static final int REFRESH_ICON=2;
	
	@Override
	public void init() {

	}
	
	private ProductItemAdapter  productAdapter;

	@Override
	public void refresh(Object... param) {
		switch(((Integer)param[0]).intValue())
		{
		case -100://
			Toast.makeText(this,"获取失败", 1000).show();
			break;
		case REFRESH_PRODUCT:
			List<Map<String,Object>>  contents = (ArrayList<Map<String,Object>> )param[1];
//			String [] from = new String[]{"item_image","title","num","valid_thru"};
			String [] from = new String[]{"title","num","valid_thru"};
			int[]  to = new int[]{R.id.onSaleItem_title,R.id.onSaleItem_num,R.id.onSaleItem_list_time};
	       productAdapter = new ProductItemAdapter(this,contents, R.layout.onsale_product_item,  from, to,this.getListView());
	       setListAdapter(productAdapter);
	       getStuffApplication().allActivity.remove(this);
		}
	}
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Map<String, Object> map = (HashMap<String, Object>) l.getItemAtPosition(position);
        String productId = map.get("productId").toString();
    	startProductDetailInfoActivity(productId);
	}
	
	/**
	 * 跳转到商品详细信息中
	 * @author wanggj
	 */
	private void startProductDetailInfoActivity(String productId){
		Intent intent = new Intent();
		intent.putExtra("productId", productId);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("num_id", productId) ;
		Task task  =new Task(Task.TASK_GET_PRODUCT_INFO,param);
		 getStuffApplication().addTask(task);
		intent.setClass(ProductItem.this, RecommendItemDetailInfoActivity.class);
		startActivity(intent);
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  setContentView(R.layout.onsaleitem_list);
	  	  //添加到Activity组件集合
		  getStuffApplication().allActivity.add(this);
	}

}
